package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.BannerQuery;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//后台使用传统的风格，不使用restful风格
@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    //查询操作
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")BannerQuery qo){
        //先查询出所有的地区数据 创造个查询所有的方法
        // 然后共享到banner/list.ftl页面
        //使用mybatis框架用的是pageHelper分页插件的PageInfo类封装七参数，现在使用mongodb数据库
        //我们得使用mongodb提供的分页插件中的Page对象
        Page<Banner> page=bannerService.query(qo);
        model.addAttribute("page",page);
        return "banner/list";
    }


    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(){
        List<Strategy> sts = strategyService.list();
        List<Travel> ts = travelService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("sts",sts);
        map.put("ts",ts);
        return JsonResult.success(map);
    }

    //保存编辑操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        //定义保存或更新方法，返回JsonResult对象
        bannerService.saveOrUpdate(banner);
        return JsonResult.success();
    }


    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        //执行删除操作，返回浏览器json格式结果
        bannerService.delete(id);
        return JsonResult.success();
    }

}
